define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$filter', '$mdDialog', 'config', 'MMWService', '$calculater', '$gallery', '$timeout',
        function ($scope, $rootScope, $filter, $mdDialog, config, $MMWService, $calculater, $gallery, $timeout) {
            $scope.init = function () {
                $scope.qc_item = 'check_project';
                $scope.re_apply_qc = false;
                $scope.datalist = [];
                $scope.badCodeList = [];
                $scope.condition = {
                    op_no : '',
                    op_name: '',
                    op_display : '',
                    machine_no: '',
                    serial_number: '',
                    station_no: '',
                    application_start: null,
                    application_end: null,
                    application_person: '',
                    plot_no: ''
                };
                var settings = JSON.parse(localStorage.getItem(config.appName + '_B0501_favourite_op_no') || '{}');
                $scope.condition.op_no = settings.op_no || '';
                $scope.condition.op_name = settings.op_name || '';
                if ($scope.condition.op_no != '') {
                    $scope.condition.op_display = settings.op_no + ' (' + settings.op_name + ')';
                }
                $scope.applicationLog = [];
                $scope.inspectionInfo = {
                    defect_qty: 0,
                    area_no: '',
                    qc_reason_detail: [],
                    checklist_detail: []
                }
            };

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            $scope.switchItem = function (item) {
                $scope.qc_item = item;
            };

            $scope.favorite = function(callback){
                if(config.setting.OperatingArea==''){
                    $scope.$root.showAlert($filter('translate')('home.showAlert'));
                    return;
                }
                $MMWService.showQueryListPNIG({
                    page_mode: 4,
                    mode:'M',
                    key:'page_no',
                    header : [
                        {label: $filter('translate')('common.queryList.op_c'), code:'page_no'},
                        {label: $filter('translate')('common.queryList.op_n'), code:'page_name'}
                    ],
                    selectKey:{key:'favourite', true:'1', false:'0'},
                    setCondition:function(conditions){
                        conditions.push({column_name:'B.MASTERNO', query_mode:'0', value:config.setting.OperatingArea, merge_condition_model:'AND' });
                        return conditions;
                    },
                    confirm: function(selectItem, dialog){
                        var content = {
                            master_type: 5,
                            master_no :config.setting.OperatingArea,
                            favourite_qc_op_detail:[]
                        };
                        if(selectItem && selectItem.length>0){
                            selectItem.forEach(function(item){
                                if(item.favourite == '1'){
                                    content.favourite_qc_op_detail.push({op_no:item.page_no});
                                }
                            });
                        }
                        beforeSaveFavorite(content.favourite_qc_op_detail, function(){
                            $MMWService.sendToServer({
                                uri: 'ServicesSTD.Module_SYS.favourite_machine_process',
                                content : content,
                                success : function(data){
                                    dialog.hide();
                                    if(callback)
                                        callback();
                                }
                            });
                        });
                    }
                });
            };

            function beforeSaveFavorite(favoriteList, callback){
                var isDeleteCurrentOp = false;
                if($scope.condition.op_no && $scope.condition.op_no != ''){
                    if(favoriteList && favoriteList.length > 0){
                        var filterList = $filter('filter')(favoriteList, {op_no: $scope.condition.op_no}, true);
                        if(filterList.length == 0){
                            isDeleteCurrentOp = true; //使用者將"目前的作業站"從我的最愛移除了
                        }
                    }
                }

                if(isDeleteCurrentOp){
                    $scope.$root.showAlert($filter('translate')('B1501.msg.favorite_remove'),function(){
                        $scope.condition.op_no = '';
                        $scope.condition.op_name = '';
                        $scope.condition.machine_no = '';
                        $scope.condition.op_display = '';
                        $scope.condition.plot_no = '';
                        $scope.inspectionInfo = undefined;
                        localStorage.setItem(config.appName + '_B0501_favourite_op_no', JSON.stringify({op_no:$scope.condition.op_no, op_name:$scope.condition.op_name}));
                        if(callback)
                            callback();
                    });
                }else{
                    if(callback)
                        callback();
                }
            }

            $scope.confirm = function () {
                if ($scope.datalist.filter(d => d.is_select).length == 0) {
                    $scope.$root.showAlert($filter('translate')('common.msg.plz_select_one'));
                    return;
                }
                var defect_qty = 0, msg = '';
                //20200831 modify by Dustdusk for M#77210:
                $scope.inspectionInfo.qc_reason_detail.forEach(function(item, index){
                    //此項目須判斷是否要輸入不良
                    // 20231221 modify by wenwei for M#153794: [MSSQL] [H5][首巡自末检]质量检验检验项目不合格是否需要录入不良问题
                    if(item.is_inputscrap == '1'){
                        // switch btn 沒選過 / false
                        if(item.check_type == 0 && (item.reason_value == '' || item.reason_value == 'false')){
                            // 檢查不良現象是否有新增不良原因
                            if(dialog.inspectionInfo.checklist_detail.length == 0){
                                msg += $filter('translate')('B0207.check_list.column1') + item.reason_no + '、';
                            }
                        } else if(item.check_type == 1){
                            // 有輸入值
                            if(item.reason_value !== '' && item.reason_value !== null) {
                                // 符合標準
                                if((+item.mini_value) <= (+item.reason_value) && (+item.reason_value) <= (+item.max_value)) return
                            }
                            // 檢查不良現象是否有新增不良原因
                            if(dialog.inspectionInfo.checklist_detail.length == 0){
                                msg += $filter('translate')('B0207.check_list.column1') + item.reason_no + '、';
                            }
                        }
                    }
                });

                if(msg != ''){
                    if($scope.inspectionInfo.checklist_detail.length == 0){
                        $scope.$root.showAlert(msg + $filter('translate')('B0207.msg.checkitem_need_badreason'));
                        return;
                    } else {
                        msg = '';
                    }
                }

                $scope.inspectionInfo.checklist_detail.forEach(function(item, index){
                    item.item_seq = index;
                    defect_qty = $calculater.decimalAdd(defect_qty, item.item_qty);
                });

                let selectRow = $scope.datalist.filter(d => d.is_select);
                var sumQty = selectRow[0].check_qty;
                if ((+defect_qty) > (+sumQty)) {
                    msg = $filter('translate')('B1501.msg.check_qty_lt_defect_qty');
                } 

                if (msg == '') {
                    $scope.inspectionInfo.defect_qty = defect_qty;
                    firstPieceCheckProcess();
                } else {
                    $scope.$root.showAlert(msg);
                }
            };

            // 修改复检申请
            $scope.change_re_apply_qc = function () {
                $scope.re_apply_qc = !$scope.re_apply_qc;
            };

            //查詢 - 作業站開窗
            $scope.loadOpNo = function () {
                $scope.$root.showQueryList({
                    header: [
                        {label:$filter('translate')('common.queryList.op_c'), code:'page_no'},
                        {label:$filter('translate')('common.queryList.op_n'), code:'page_name'}
                    ],
                    query: function(conditions, feedback){
                        var content = {
                            page_mode: 4,
                            query_condition_info: []
                        };
                        if(config.setting.OperatingArea != ''){
                            content.query_condition_info.push({column_type:'29', query_mode:'0', value:config.setting.OperatingArea, merge_condition_model:'AND' });
                        }
                        //20180525 modify by 李栋 for mantis#0047129
                        $MMWService.pageNumberInfoGet(
                            {
                                page_mode: 4,
                                query_condition_info:[
                                    {column_name:'B.MASTERNO', query_mode:'0', value:config.setting.OperatingArea, merge_condition_model:'AND' }
                                ]
                            },function(data){
                                var temp = $filter('filter')(data, {favourite:'1'});
                                //var temp = data.page_number_info_get;
                                if(temp && temp.length){
                                    feedback(temp);
                                } else {
                                    $scope.$root.showConfirm($filter('translate')('B1501.msg.favorite_empty'), function(confirmDialog){
                                        confirmDialog.hide();
                                        $timeout(function(){
                                            $scope.favorite(function(){
                                                $scope.loadOpNo();
                                            });
                                        }, 200);
                                    });
                                }
                            }
                        );
                    },
                    selectCode:$scope.condition.op_no||'',
                    confirm : function(selectItem, dialog){
                        $scope.condition.op_display = selectItem.page_no + ' ('+selectItem.page_name+')';
                        $scope.condition.op_no = selectItem.page_no;
                        $scope.condition.op_name = selectItem.page_name;
                        localStorage.setItem(config.appName + '_B0501_favourite_op_no', JSON.stringify({op_no:$scope.condition.op_no, op_name:$scope.condition.op_name}));
                        dialog.hide();
                    }
                });
            };

            //查詢 - 生產批開窗
            $scope.loadPlotInfo = function () {
                $MMWService.showQueryListPNIG({
                    page_mode: 45,
                    condition: [
                        { name: $filter('translate')('common.queryList.plot_no'), column_name: 'LotNo', query_mode: '2', value: '' }
                    ],
                    setCondition: function (conditions) {
                        conditions.push({ column_name: 'OPNO', query_mode: '0', value: $scope.condition.op_no, merge_condition_model: 'AND' });
                        conditions.push({ column_name: 'ApplyQcType', query_mode: '0', value: 5, merge_condition_model: 'AND' });
                        return conditions;
                    },
                    header: [
                        {label:$filter('translate')('common.queryList.plot_no'), code:'page_no'}
                    ],
                    selectCode:$scope.condition.plot_no,
                    confirm : function(selectItem){
                        $scope.condition.plot_no = selectItem.page_no;
                    }
                });
            };

            //设备编号开窗
            $scope.loadMachineNo = function () {
                $MMWService.showQueryListPNIG({
                    page_mode: 46,
                    condition: [
                        { name: $filter('translate')('common.queryList.machine_c'), column_name: 'a.EquipmentNo', query_mode: '2', value: '' }
                    ],
                    setCondition: function (conditions) {
                        conditions.push({ column_name: 'a.OPNO', query_mode: '0', value: $scope.condition.op_no, merge_condition_model: 'AND' });
                        if ($scope.condition.plot_no != '') {
                            conditions.push({ column_name: 'a.LOTNO', query_mode: '0', value: $scope.condition.plot_no, merge_condition_model: 'AND' });
                        }
                        conditions.push({ column_name: 'ApplyQcType', query_mode: '0', value: 5, merge_condition_model: 'AND' });
                        return conditions;
                    },
                    header: [
                        { label: $filter('translate')('common.queryList.machine_c'), code: 'page_no' },
                        { label: $filter('translate')('common.queryList.machine_n'), code: 'page_name' }
                    ],
                    selectCode: $scope.condition.machine_no,
                    confirm: function (selectItem) {
                        $scope.condition.machine_no = selectItem.page_no;
                    }
                });
            };

            // 工位开窗
            $scope.loadStationNo = function () {
                var query_condition_info = [];
                query_condition_info.push({ column_name: 'a.OPNO', query_mode: '0', value: $scope.condition.op_no, merge_condition_model: 'AND' });
                query_condition_info.push({ column_name: 'ApplyQcType', query_mode: '0', value: 5, merge_condition_model: 'AND' });
                if ($scope.condition.plot_no != '') {
                    query_condition_info.push({ column_name: 'a.LOTNO', query_mode: '0', value: $scope.condition.plot_no, merge_condition_model: 'AND' });
                }
                $MMWService.pageNumberInfoGet({
                    page_mode: '47',
                    query_condition_info: query_condition_info
                }, function (response) {
                    $scope.showSelect({
                        title: $filter('translate')('common.msg.plz_select'),
                        label: function (item) {
                            return item.page_no;
                        },
                        code: 'page_no',
                        selectCode: $scope.condition.station_no,
                        list: response,
                        confirm: function (selected, d) {
                            d.hide();
                            $scope.condition.station_no = selected.page_no;
                        }
                    });
                })
            }

            //申请人开窗
            $scope.loadApplicationNo = function () {
                $MMWService.showQueryListPNIG({
                    page_mode: 48,
                    condition: [
                        { name: $filter('translate')('common.queryList.employee_c'), column_name: 'a.Creator', query_mode: '2', value: '' }
                    ],
                    setCondition: function (conditions) {
                        conditions.push({ column_name: 'a.OPNO', query_mode: '0', value: $scope.condition.op_no, merge_condition_model: 'AND' });
                        conditions.push({ column_name: 'ApplyQcType', query_mode: '0', value: 5, merge_condition_model: 'AND' });
                        if ($scope.condition.plot_no != '') {
                            conditions.push({ column_name: 'a.LOTNO', query_mode: '0', value: $scope.condition.plot_no, merge_condition_model: 'AND' });
                        }
                        return conditions;
                    },
                    header: [
                        { label: $filter('translate')('common.queryList.employee_c'), code: 'page_no' },
                        { label: $filter('translate')('common.queryList.employee_n'), code: 'page_name' }
                    ],
                    selectCode: $scope.condition.application_person,
                    confirm: function (selectItem) {
                        $scope.condition.application_person = selectItem.page_no;
                    }
                });
            };

            //成品序号开窗
            $scope.loadSerialNo = function () {
                var query_condition_info = [];
                query_condition_info.push({ column_name: 'a.OPNO', query_mode: '0', value: $scope.condition.op_no, merge_condition_model: 'AND' });
                query_condition_info.push({ column_name: 'ApplyQcType', query_mode: '0', value: 5, merge_condition_model: 'AND' });
                if ($scope.condition.plot_no != '') {
                    query_condition_info.push({ column_name: 'a.LOTNO', query_mode: '0', value: $scope.condition.plot_no, merge_condition_model: 'AND' });
                }
                $MMWService.pageNumberInfoGet({
                    page_mode: '49',
                    query_condition_info: query_condition_info
                }, function (response) {
                    $scope.showSelect({
                        title: $filter('translate')('common.msg.plz_select'),
                        label: function (item) {
                            return item.page_no;
                        },
                        code: 'page_no',
                        selectCode: $scope.condition.serial_number,
                        list: response,
                        confirm: function (selected, d) {
                            d.hide();
                            $scope.condition.serial_number = selected.page_no;
                        }
                    });
                })
            };

            $scope.QRcodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        if(result.text && result.text!=''){
                            var temp = result.text.split(';');
                            if(temp.length>1){
                                $scope.condition.op_no = temp[1]||'';
                            }
                            if(temp.length>0){
                                $scope.condition.plot_no = temp[0]||'';
                            }
                            $scope.search();
                        }
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('B0214.showAlert.column3'));
                    },
                    orientation : 'landscape'
                });
            }

            //查詢 - 確認
            $scope.search = function () {
                if ($scope.condition.op_no.length == 0) {
                    $scope.$root.showAlert($filter('translate')('B1501.msg.plot_no_empty'));
                } else {
                    // 查询待检清单
                    var content = {
                        apply_qc_type: '5',
                        op_no: $scope.condition.op_no,
                        lot_no: $scope.condition.plot_no,
                        equipment_no: $scope.condition.machine_no,
                        position_no: $scope.condition.station_no,
                        creator: $scope.condition.application_person,
                        application_start: $scope.condition.application_start ? FormatDate($scope.condition.application_start) : '',
                        application_end: $scope.condition.application_end ? FormatDate($scope.condition.application_end) : '',
                        pcs_no: $scope.condition.serial_number
                    }
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_WIP.piece_check_get',
                        content: content,
                        success: function (data) {
                            if (data.pieceList.length == 0) {
                                $scope.datalist = [];
                                $scope.picSrcArray = [];
                                $scope.applicationLog = [];
                                $scope.inspectionInfo.qc_reason_detail = [];
                                $scope.inspectionInfo.checklist_detail = [];
                                $scope.badCodeList = [];
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            } else {
                                data.pieceList.forEach(p => {
                                    if (p.qc_result == 'Y') {
                                        p.qc_result_n = $filter('translate')('B1501.qualified');
                                    } else if (p.qc_result == 'N') {
                                        p.qc_result_n = $filter('translate')('B1501.unqualified');
                                    }
                                    if (p.qc_item_result == 'Y') {
                                        p.qc_item_result_n = $filter('translate')('B1501.qualified');
                                    } else if (p.qc_item_result == 'N') {
                                        p.qc_item_result_n = $filter('translate')('B1501.unqualified');
                                    }
                                });
                                $scope.datalist = data.pieceList;
                                $scope.datalist.forEach(d => d.is_select = false);
                                $scope.datalist[0].is_select = true;
                                $scope.itemClick($scope.datalist[0]);
                            }
                        }
                    });
                }
            };

            $scope.changeFix = function (item) {
                $scope.datalist.forEach(d => d.is_select = false);
                item.is_select = true;
                $scope.itemClick(item);
            }

            $scope.itemClick = function (item) {
                $scope.inspectionInfo.checklist_detail = [];
                $MMWService.pageNumberInfoGet({
                    page_mode : '15',
                    query_condition_info : [
                        { column_name: 'OPNo', query_mode: '0', value: item.op_no }
                    ]
                }, function(itemList){
                    $scope.badCodeList = itemList;
                    opqcreason_info_get({ plot_no: item.lot_no, op_no: item.op_no }, function (data) {
                        $scope.server_url = 'http://' + config.server.ip + ':' + config.server.port;
                        $scope.inspectionInfo.qc_reason_detail = data.qc_reason_detail || [];
                        $scope.inspectionInfo.area_no = data.area_no;
                        // 检验人记录
                        $MMWService.pageNumberInfoGet({
                            page_mode: '50',
                            query_condition_info: [
                                { column_name: 'A.QCFormNo', query_mode: '0', value: item.qc_form_no }
                            ]
                        }, function (dataLog) {
                            if (dataLog.length > 0) {
                                dataLog.forEach(d => {
                                    if (d.inputvalue == 'true') {
                                        d.inputvalue_n = $filter('translate')('B1501.qualified');
                                    } else if (d.inputvalue == 'false') {
                                        d.inputvalue_n = $filter('translate')('B1501.unqualified');
                                    }
                                });
                            }
                            $scope.applicationLog = dataLog || [];
                        });
                    });
                });
            };

            function FormatDate(date) {
                let seperator1 = "/";
                let year = date.getFullYear();
                let month = date.getMonth() + 1;
                let strDate = date.getDate();
                if (month >= 1 && month <= 9) {
                    month = "0" + month;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                let currentdate = year + seperator1 + month + seperator1 + strDate;
                return currentdate;
            }

            //不良原因開窗
            $scope.loadInspectionReasonList = function (item) {
                // 是否选中单头数据
                if ($scope.datalist.filter(d => d.is_select).length == 0){
                    $scope.$root.showAlert($filter('translate')('common.msg.plz_select_one'));
                    return;
                }
                $scope.$root.showQueryList({
                    key:'page_no',
                    selectCode:item?item.page_no:'',
                    header : [
                        {label: $filter('translate')('common.queryList.badReason_c'), code:'page_no'},
                        {label: $filter('translate')('common.queryList.badReason_n'), code:'page_name'}
                    ],
                    query: function(conditions, feedback){
                        feedback($scope.badCodeList);
                    },
                    confirm: function (selectItem, radioDialog) {
                        if(item != undefined){
                            item.item_code = selectItem.page_no;
                            item.page_no = selectItem.page_no;
                            item.page_name = selectItem.page_name;
                        } else {
                            $scope.inspectionInfo.checklist_detail.push({
                                item_code: selectItem.page_no,
                                page_no : selectItem.page_no,
                                page_name : selectItem.page_name,
                                item_qty: 1,
                                item_seq: ''
                            });
                        }
                        radioDialog.hide();
                    }
                });
            };

            $scope.addRow = function () {
                $scope.loadInspectionReasonList();
            };

            $scope.removeRow = function (index) {
                $scope.inspectionInfo.checklist_detail.splice(index, 1);
            };

            $scope.input_defect_qty = function (event, item) {
                var target = event.currentTarget;
                angular.element(target).addClass('is-focus');
                $calculater.open(function (num, calDialog) {
                        angular.element(target).removeClass('is-focus');
                        calDialog.hide();
                        item.item_qty = num;
                        //$scope.inspectionInfo.defect_qty = $scope.inspectionInfo.defect_qty + parseInt(num) - parseInt(oriNum);

                    },
                    function (calDialog) {
                        angular.element(target).removeClass('is-focus');
                        calDialog.hide();
                    },
                    {
                        defaultNum: item.item_qty,
                        decimalLength: 3
                    });
            };

            $scope.InRange = function(item){
                if(item.check_type == 1 && !isNaN(item.reason_value)){
                    if((+item.mini_value) <= (+item.reason_value)
                        && (+item.reason_value) <= (+item.max_value)){
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            };

            //送出結果
            function firstPieceCheckProcess() {
                // 获取选中行
                let selectRow = $scope.datalist.filter(d => d.is_select);
                var content = {
                    machine_no: selectRow[0].equipment_no ? selectRow[0].equipment_no : 'N/A',
                    pcsno: selectRow[0].pcs_no,
                    position_no: selectRow[0].position_no,
                    station_no: $scope.condition.op_no,
                    op_no: $scope.condition.op_no,
                    plot_no: selectRow[0].lot_no,
                    employee_no: config.cache.account,
                    creator: config.cache.account,
                    area_no: $scope.inspectionInfo.area_no,
                    check_qty: selectRow[0].check_qty,
                    defect_qty: $scope.inspectionInfo.defect_qty,
                    check_time: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
                    origin_qc_form_no: selectRow[0].qc_form_no,
                    qc_result: 'Y',
                    apply_qc_type: $scope.re_apply_qc ? '6' : '',
                    qc_type: '-7',
                    type: '5',
                    checklist_detail: $scope.inspectionInfo.checklist_detail,
                    image_detail: [],
                    qc_reason_detail: $scope.inspectionInfo.qc_reason_detail
                };

                if((+content.defect_qty) != 0)
                    content.qc_result = 'N';

                for(var i=0;i<$scope.picSrcArray.length;i++){
                    var picSrc = $scope.picSrcArray[i];
                    content.image_detail.push({
                        image_src:picSrc,
                        image_seq:i,
                        image_format:'jpg'
                    });
                }
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.first_piece_check_process',
                    content: content,
                    success: function () {
                        $scope.$root.showToast($filter('translate')('common.msg.success'));
                        $scope.re_apply_qc = false;
                        $scope.condition.machine_no = '';
                        $scope.condition.plot_no = '';
                        $scope.condition.station_no = '';
                        $scope.condition.application_start = null;
                        $scope.condition.application_end = null;
                        $scope.condition.serial_number = '';
                        $scope.condition.application_person = '';
                        $scope.datalist = [];
                        $scope.picSrcArray = [];
                        $scope.applicationLog = [];
                        $scope.inspectionInfo.qc_reason_detail = [];
                        $scope.inspectionInfo.checklist_detail = [];
                        $scope.badCodeList = [];
                        // 重新查询刷新
                        $scope.search();
                    }
                });
            }

            function opqcreason_info_get(content, feedback) {
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.op_qcreason_info_get',
                    content: {
                        op_no: content.op_no,
                        lot_no: content.plot_no,
                        machine_no: '',
                        area_no: '',
                        type: '5'
                    },
                    success: function (data) {
                        if(feedback)
                            feedback(data);
                    }
                });
            }

            var picLimitNum = 5;
            $scope.picSrcArray = [];
            //拍照
            $scope.pic_add = function($event){
                if($scope.picSrcArray.length < picLimitNum){
                    if(typeof(navigator.camera) !== 'undefined'){
                        //回傳URI
                        navigator.camera.getPicture(
                            function(imageURI) {              
                                if($rootScope.compressImage)
                                {
                                    $rootScope.compressImage(imageURI, 
                                        function(data){  
                                            $scope.picSrcArray.push(data);
                                            $scope.$apply();
                                            }); 
                                }
                                else
                                {
                                    $scope.picSrcArray.push(imageURI);
                                    $scope.$apply();
                                }
                                //$scope.data.scrollLeft = document.getElementById('img-tile-list').clientWidth;
                            },
                            function(message) {
                                //alert('Failed because: ' + message);
                            },
                            {
                                quality: 50,
                                destinationType: Camera.DestinationType.DATA_URL
                            }
                        );
                    } else if(window.nodeRequire){
                        //回傳URI
                        $scope.$root.FileDialog({
                            callback:function(fileList){
                                $scope.$root.showLoading();
                                $scope.$apply();
                                setTimeout(function(){
                                    if(fileList){
                                        if(fileList.length == 0){
                                            //
                                        } else if(($scope.picSrcArray.length + fileList.length) > picLimitNum){
                                            $scope.$root.showAlert($filter('translate')('B1501.msg.pic_limit', {num:picLimitNum}));
                                        } else {
                                            var fs = window.nodeRequire('fs');
                                            fileList.forEach(function(fileName){
                                                var image = fs.readFileSync(fileName);
                                                if(image)
                                                {               
                                                    if($rootScope.compressImage)
                                                    {
                                                        // 20231215 modify by Alan for#153209 : 使用$rootScope要記得引用，圖片壓縮完要用$apply()更新畫面
                                                        $rootScope.compressImage(new Buffer(image).toString('base64'), (data) => {
                                                            $scope.picSrcArray.push(data); 
                                                            $scope.$apply();
                                                        }); 
                                                    }
                                                    else
                                                    {
                                                        $scope.picSrcArray.push(new Buffer(image).toString('base64'));
                                                        $scope.$apply();
                                                    }
                                                }
                                            });
                                        }
                                    }
                                    $scope.$root.hideLoading();
                                    $scope.$apply();
                                });
                            }
                        });
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    }
                } else {
                    $scope.$root.showAlert($filter('translate')('B1501.msg.pic_limit', {num:picLimitNum}));
                }
            };

            $scope.pic_delete = function(index){
                $scope.picSrcArray.splice(index,1);
            };

            $scope.pic_open = function(index){
                $gallery.show('#'+(index+1), 'data:image/jpeg;base64,'+$scope.picSrcArray[index], function(){});
            };

            $scope.open_checkPic = function(item){
                $gallery.show(item.reason_no, $scope.server_url+item.picture_path, function(){});
            };
        }
    ];
});
